local zhijian = fk.CreateSkill{
  name = "lingling__zhijian",
}

Fk:loadTranslationTable{
  ["lingling__zhijian"] = "直谏",
  [":lingling__zhijian"] = "弃牌阶段结束时，若你本阶段弃置了牌，你可以视为使用一张无距离限制的【杀】，若造成伤害，受伤角色获得你弃置的牌。",

  ["#lingling__zhijian-invoke"] = "直谏：你可以视为使用一张无距离限制的【杀】，若造成伤害，目标获得你本阶段弃置的牌",
}

zhijian:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhijian.name) and player.phase == Player.Discard and
      #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerJudge then
                return true
              end
            end
          end
        end
      end, Player.HistoryPhase) > 0 and
      player:canUse(Fk:cloneCard("slash"), {bypass_distances = true, bypass_times = true})
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "slash",
      skill_name = zhijian.name,
      prompt = "#lingling__zhijian-invoke",
      cancelable = true,
      extra_data = {
        bypass_distances = true,
        bypass_times = true,
        extraUse = true,
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).extra_data
    room:useCard(use)
    if use and use.damageDealt then
      local ids = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(room.discard_pile, info.cardId) then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end, Player.HistoryPhase)
      if #ids > 0 then
        for _, p in ipairs(use.tos) do
          if not p.dead then
            room:moveCardTo(ids, Card.PlayerHand, p, fk.ReasonJustMove, zhijian.name, nil, true, p)
            return
          end
        end
      end
    end
  end,
})

return zhijian
